package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.SelectBlockTypes;
import com.taptech.doufu.bean.choice.ChoiceTopicBean;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.choice.ChoiceCategoryRecommendView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicColumnView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicComicView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicImageView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicLimitView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicNewUserView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicNormalView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicComplexView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicRankComplexView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicRecommendView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicStoryView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicTagView;
import com.taptech.doufu.ui.view.choice.ChoiceTopicThemeView;
import com.taptech.doufu.ui.view.topicview.AuthorFiveItemView;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;

import java.util.ArrayList;
import java.util.List;

/**
 * 小说列表适配器
 */
public class DFChoiceAdapter extends BaseAdapter {

    public static final int TYPE_ITEM_COUNT = 16;

    private Context context;
    private List<ChoiceTopicBean> contentList = new ArrayList<>();
    private PullToRefreshListView listView;


    public DFChoiceAdapter(Context context, List<ChoiceTopicBean> contentList, PullToRefreshListView listView) {
        this.context = context;
        if (contentList != null) {
            this.contentList = contentList;
        }
        this.listView = listView;
    }

    public void refrushList(List<ChoiceTopicBean> contentList) {
        if (contentList != null) {
            this.contentList = contentList;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (contentList != null) {
            return contentList.size();
        }else{
            return 0;
        }
    }


    @Override
    public ChoiceTopicBean getItem(int position) {
        if (contentList != null && contentList.size() > position) {
            return contentList.get(position);
        }else{
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_ITEM_COUNT;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ChoiceTopicBean model = getItem(position);
        model.setListPos(position); //设置卡片在列表中的位置
        int type = getItemViewType(position);
        if (type == SelectBlockTypes.TAGS) {
            if (convertView == null) {
                convertView = new ChoiceTopicTagView(context, model);
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicTagView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == SelectBlockTypes.COMICS) {
            if (convertView == null) {
                convertView = new ChoiceTopicComicView(context, model);
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicComicView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == SelectBlockTypes.NEW_BOOKS) {
            if (convertView == null) {
                convertView = new ChoiceTopicColumnView(context, model);
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicColumnView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == SelectBlockTypes.NOTICE_BLOCK) {
            if (convertView == null) {
                convertView = new ChoiceTopicImageView(context, model.getBannerModel(),model.getTitle(),model.getEventData());
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicImageView(context, model.getBannerModel(),model.getTitle(),model.getEventData());
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == SelectBlockTypes.TAB2X3) {
            String tag = "complex" + position;
            if (convertView == null) {
                convertView = new ChoiceTopicComplexView(context, model);
                convertView.setTag((tag));
            } else {
                if (convertView.getTag().toString().equals(tag)) {
                } else {
                    convertView = new ChoiceTopicComplexView(context, model);
                    convertView.setTag((tag));

                }
            }
        } else if (type == SelectBlockTypes.RANKS) {
            String tag = "ranks" + position;
            if (convertView == null) {
                convertView = new ChoiceTopicRankComplexView(context, model);
                convertView.setTag((tag));
            } else {
                if (convertView.getTag().toString().equals(tag)) {
                } else {
                    convertView = new ChoiceTopicRankComplexView(context, model);
                    convertView.setTag((tag));
                }
            }
        } else if (type == SelectBlockTypes.LIMITED_TIME_FREE) {
            if (convertView == null) {
                convertView = new ChoiceTopicLimitView(context, model);
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicLimitView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == SelectBlockTypes.THEME) {
            if (convertView == null) {
                convertView = new ChoiceTopicThemeView(context,model);
                convertView.setTag((model.getTitle()));
            }else{
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicThemeView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == SelectBlockTypes.STORY) {
            if (convertView == null) {
                convertView = new ChoiceTopicStoryView(context,model);
                convertView.setTag((model.getTitle()));
            }else{
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicStoryView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if (type == SelectBlockTypes.NEW) {




        } else if (type == SelectBlockTypes.RECOMMEND) {
            if (convertView == null) {
                convertView = new ChoiceTopicRecommendView(context,model,listView);
                convertView.setTag((model.getTitle()));
            }else{
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicRecommendView(context, model,listView);
                    convertView.setTag((model.getTitle()));
                }
            }
        } else if(type == SelectBlockTypes.NEW_USER_GIFT){
            if (convertView == null) {
                convertView = new ChoiceTopicNewUserView(context, model.getNewUserGiftModel(), R.layout.view_new_user_gift);
                convertView.setTag(model.getTitle());
            }else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                }else{
                    convertView = new ChoiceTopicNewUserView(context, model.getNewUserGiftModel(),R.layout.view_new_user_gift);
                    convertView.setTag(model.getTitle());
                }
            }
        } else if (type == SelectBlockTypes.CATEGORY_RECOMMEND) {
            if (convertView == null) {
                convertView = new ChoiceCategoryRecommendView(context, model);
                convertView.setTag(model.getTitle());
            }else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                }else{
                    convertView = new ChoiceCategoryRecommendView(context, model);
                    convertView.setTag(model.getTitle());
                }
            }
        }  else if (type == SelectBlockTypes.STAR_AUTHOR) {
            if (convertView == null) {
                convertView = new AuthorFiveItemView(context, model);
                convertView.setTag(model.getTitle());
            }else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                }else{
                    convertView = new AuthorFiveItemView(context, model);
                    convertView.setTag(model.getTitle());
                }
            }
        } else {
            if (convertView == null) {
                convertView = new ChoiceTopicNormalView(context, model);
                convertView.setTag((model.getTitle()));
            } else {
                if (convertView.getTag().toString().equals(model.getTitle())) {
                } else {
                    convertView = new ChoiceTopicNormalView(context, model);
                    convertView.setTag((model.getTitle()));
                }
            }
        }
        return convertView;
    }
}
